//  添加日常指标
//  AddViewController.m
//  yilutongxing
//
//  Created by Robin-Pc on 2017/5/9.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "AddViewController.h"
#import <ActionSheetDatePicker.h>
#import <QMUITips.h>

@interface AddViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (strong, nonatomic) UILabel *checkDate;//检查日期
@property (strong, nonatomic) UILabel *checkTime;//检查时间
@property (strong, nonatomic) IBOutlet UITableViewCell *cell1;
@property (strong, nonatomic) IBOutlet UITableViewCell *cell2;
@property (strong, nonatomic) IBOutlet UITableViewCell *cell3;
@property (strong, nonatomic) IBOutlet UITableViewCell *cell4;
@property (strong, nonatomic) IBOutlet UITableViewCell *cell5;
@property (strong, nonatomic) IBOutlet UITableViewCell *cell6;
@property (strong, nonatomic) IBOutlet UITableViewCell *cell7;

@property (strong, nonatomic) ActionSheetDatePicker *date_picker;
@property (strong, nonatomic) ActionSheetDatePicker *time_picker;

@end

#define URL_Post @"/api/dailyIndicator"

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.addBtn.layer.cornerRadius = 20;
    self.addBtn.layer.masksToBounds = YES;
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    self.title = @"添加";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    //self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)];
}
-(void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)addAction:(id)sender {
    //从UI中取数据
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if ([_checkDate.text isNotEmpty]) {
        [param setObject:_checkDate.text forKey:@"date"];
    }
    if ([_checkTime.text isNotEmpty]) {
        [param setObject:_checkTime.text forKey:@"time"];
    }
    UITextField *highPressure = [_cell1 viewWithTag:1];
    if ([highPressure.text isNotEmpty]) {
        [param setObject:highPressure.text forKey:@"highPressure"];
    }
    UITextField *lowPressure = [_cell1 viewWithTag:2];
    if ([lowPressure.text isNotEmpty]) {
        [param setObject:lowPressure.text forKey:@"lowPressure"];
    }
    UITextField *bloodSugar = [_cell2 viewWithTag:1];
    if ([bloodSugar.text isNotEmpty]) {
        [param setObject:bloodSugar.text forKey:@"bloodSugar"];
    }
    UITextField *bloodSugar2 = [_cell3 viewWithTag:1];
    if ([bloodSugar2.text isNotEmpty]) {
        [param setObject:bloodSugar2.text forKey:@"bloodSugar2"];
    }
    UITextField *urine = [_cell4 viewWithTag:1];
    if ([urine.text isNotEmpty]) {
        [param setObject:urine.text forKey:@"urine"];
    }
    UITextField *weight = [_cell6 viewWithTag:1];
    if ([weight.text isNotEmpty]) {
        [param setObject:weight.text forKey:@"weight"];
    }
    UITextField *tem = [_cell5 viewWithTag:1];
    if ([tem.text isNotEmpty]) {
        [param setObject:tem.text forKey:@"temperature"];
    }
    UITextField *height = [_cell7 viewWithTag:1];
    if ([height.text isNotEmpty]) {
        [param setObject:height.text forKey:@"height"];
    }
    //网络请求
    [[YZENetwork sharedInstace] postRequestWithUrl:[BASE stringByAppendingString:URL_Post]
                                        parameters:param
                                          delegate:self
                                        requestTag:1
                                      successBlock:^(id responseObject) {
                                          [QMUITips showSucceed:@"保存成功!" inView:self.view hideAfterDelay:2];
                                      }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 9;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    if (indexPath.row<2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        }
        if (indexPath.row == 0) {
            cell.textLabel.text = @"日期";
            self.checkDate = cell.detailTextLabel;
            self.checkDate.text = @" ";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else{
            cell.textLabel.text = @"时间";
            self.checkTime = cell.detailTextLabel;
            self.checkTime.text = @" ";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }else if (indexPath.row == 2){
        return _cell1;
    }else if (indexPath.row == 3){
        return _cell2;
    }else if (indexPath.row == 4){
        return _cell3;
    }else if (indexPath.row == 5){
        return _cell4;
    }else if (indexPath.row == 6){
        return _cell5;
    }else if (indexPath.row == 7){
        return _cell6;
    }else{
        return _cell7;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
    switch (indexPath.row) {
        case 0:{
            self.date_picker = [ActionSheetDatePicker showPickerWithTitle:@"选择日期"
                                                        datePickerMode:UIDatePickerModeDate
                                                        selectedDate:[NSDate date]
                                                        minimumDate:[NSDate dateWithTimeIntervalSinceNow:-100*12*30*24*3600]
                                                        maximumDate:[NSDate dateWithTimeIntervalSinceNow:0]
                                                        doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
                NSDate *date = selectedDate;
                NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
                fmt.dateFormat = [NSString stringWithFormat:@"yyyy-MM-dd"];
                NSString *datestr = [fmt stringFromDate:date];
                //NSLog(@"date:%@", datestr);
                c.detailTextLabel.text = datestr;
            } cancelBlock:nil origin:self.view];
        }
            break;
        case 1:{
            self.time_picker = [ActionSheetDatePicker showPickerWithTitle:@"选择时间"
                                                        datePickerMode:UIDatePickerModeTime
                                                        selectedDate:[NSDate date]
                                                        minimumDate:[NSDate dateWithTimeIntervalSinceNow:-100*12*30*24*3600]
                                                        maximumDate:[NSDate dateWithTimeIntervalSinceNow:0]
                                                        doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
                NSDate *date = selectedDate;
                NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
                fmt.dateFormat = [NSString stringWithFormat:@"HH:mm"];
                NSString *datestr = [fmt stringFromDate:date];
                //NSLog(@"date:%@", datestr);
                c.detailTextLabel.text = datestr;
            } cancelBlock:nil origin:self.view];
        }
            break;
        default:
            break;
    }
}


@end
